package org.lizar.config.keys;

import org.lizar.json.JObject;
import org.lizar.json.JSON;

/**
 * An entry of group config.
 * 
 * @see Keys
 *
 */
public class Key {
	public String name;
	
	public Object value;
	
	public String desc;
	
	public  int getInt(){
		return (Integer)value;
	}
	
	public  long getLong(){
		try{
			return (Long)value;
		}catch(Exception e){
			if(value instanceof Integer)return (Integer)value;
			throw new RuntimeException(e);
		}
	}
	
	public  double getDouble(){
		try{
			return (Double)value;
		}catch(Exception e){
			if(value instanceof Integer)return (Integer)value;
			if(value instanceof Long)return (Long)value;
			if(value instanceof Float)return (Float)value;
			throw new RuntimeException(e);
		}
	}
	
	public  boolean _bool(){
		return (Boolean)value;
	}
	
	public String getStr(){
		return value.toString();
	}
	
	public Key clone(){
		Key k=new Key();
		k.name=name;
		k.value=value;
		k.desc=desc;
		return k;
	}
	
	public static Key to_key(JSON e){
		if(e==null)return null;
		Key k=new Key();
		k.name=e.getStr("name");
		k.value=e.get("value");
		k.desc=e.getStr("desc");
		return k;
	}
	
	public JSON to_json(){
		JSON e=new JObject();
		e.put("name", name);
		e.put("value", value);
		e.put("desc", desc);
		return e;
	}

}
